<template>
    <div id="app">
        <router-view v-if="isRouterAlive" />
    </div>
</template>

<script>
export default {
    name: 'App',
    provide () {
        return {
            reload: this.reload
        }
    },
    data () {
        return {
            isRouterAlive: true,
        }
    },
    mounted () {
        let self = this
        window.onfocus = event => {
            var islogin = this.COOKIE.getCookie('Islogin')
            if (islogin && islogin * 1 === 1) {
                // this.verify()
            }
        }
    },
    methods: {
        verify () {
            let self = this;
            this.$ajax({
                method: 'get',
                url: '/token/verify',
            }).then(res => {
                if (res.data.success) {

                } else {
                    if (res.data.resultCode === 12014) {
                        self.refreshToken()
                        return
                    }
                    this.$message({
                        message: res.data.resultInfo,
                        type: 'warning'
                    });
                    sessionStorage.clear();
                    localStorage.clear();
                    this.COOKIE.delCookie('authorization');
                    this.COOKIE.delCookie('appId');
                    this.COOKIE.delCookie('Islogin');
                    this.$router.push('/login');
                }
            })
        },
        refreshToken () {
            let self = this;
            this.$ajax({
                method: 'get',
                url: '/refresh/token',
            }).then(res => {
                let data = res.data
                if (data.success) {
                    sessionStorage.setItem('user', JSON.stringify(res.data.result.user))
                    localStorage.setItem('user', JSON.stringify(res.data.result.user))
                    self.$store.commit('user', res.data.result.user)
                    self.COOKIE.setCookie('authorization', res.data.result.jwt, 1)
                    self.COOKIE.setCookie('appId', res.data.result.user.id, 1)
                    self.COOKIE.setCookie('Islogin', 1, 1)
                }
            })
        },
        reload () {
            this.isRouterAlive = false;
            this.$nextTick(function () {
                this.isRouterAlive = true;
            })
        },
    },
}
</script>

<style>
body {
    margin: 0 !important;
    padding: 0 !important;
}
.el-input.is-disabled .el-input__inner {
    color: #8b8d91 !important;
}
</style>
